package leetcode102

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrder(root *TreeNode) [][]int {
	res := make([][]int, 0)
	if root == nil {
		return res
	}
	temp := []*TreeNode{root}
	for {
		oneLevelNode := make([]*TreeNode, 0)
		noeLevelVal := make([]int, len(temp))
		for i, node := range temp {
			noeLevelVal[i] = node.Val
			if node.Left != nil {
				oneLevelNode = append(oneLevelNode, node.Left)
			}
			if node.Right != nil {
				oneLevelNode = append(oneLevelNode, node.Right)
			}
		}

		res = append(res, noeLevelVal)
		temp = oneLevelNode
		if len(oneLevelNode) == 0 {
			break
		}
	}
	return res
}
